package com.cn.intelligence.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.cn.intelligence.service.PickManagerService;
import com.cn.intelligence.util.FtpUtil;
import com.cn.intelligence.util.IDUtils;

/**
 * Created by Administrator on 2017/4/20.
 */
@Service
public class PickManagerServiceImpl  implements PickManagerService{

    @Value("${FTP_HOST}")
    private String FTP_HOST;
    @Value("${FTP_PORT}")
    private Integer FTP_PORT;
    @Value("${FTP_USER}")
    private String FTP_USER;
    @Value("${FTP_PWD}")
    private String FTP_PWD;
    @Value("${BASE_PATH}")
    private String BASE_PATH;
    @Value("${IMAGE_SERVER}")
    private String IMAGE_SERVER;
    @Override
    public Map picFileUpload(MultipartFile uploadFile) {
        Map<String,Object> resultMap = new HashMap <String,Object>();
        try{
            // 获取文件名字
            String oldFileName = uploadFile.getOriginalFilename();
            // 生产新的文件名字
            String newFileName = IDUtils.genImageName()+oldFileName.substring(oldFileName.lastIndexOf("."));
            // 文件保存的路徑
            String imagePath = new DateTime().toString("/yyyy/MM/dd");
            System.out.print(FTP_HOST+FTP_PORT+FTP_USER+FTP_PWD+BASE_PATH+imagePath+newFileName);
            boolean result = FtpUtil.uploadFile(FTP_HOST,FTP_PORT,FTP_USER,FTP_PWD,BASE_PATH,imagePath,newFileName,uploadFile.getInputStream());
            if(result){
                resultMap.put("error",0);
                resultMap.put("url",IMAGE_SERVER+imagePath+"/"+newFileName);
            }else{
                resultMap.put("error",1);
                resultMap.put("message","文件上传失败");
            }
            return resultMap;
        }catch (Exception e){
            resultMap.put("error",1);
            resultMap.put("message","文件上传发生异常");
            return resultMap;
        }

    }
}
